package {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/* 
	*弹跳模拟 公式y=ax*x+bx+c
	*确定三点坐标就可以取得公式 起始点 最高点 落地点
	*模拟弹跳
	*非物体弹跳 只是模拟抛物线 
	*物体弹跳要有水平和垂直反向速度等
	*/
	
	[SWF(width = "610",height = "400",frameRate = "25")]
	public class Phypar extends Sprite {
		//起始坐标
		private static var x1:int;
		private static var y1:int;
		//最高点
		private static var x2:int;
		private static var y2:int;
		//落地点
		private static var x3:int;
		private static var y3:int;
		//方程变量
		private static var a;
		private static var b;
		private static var c;
		//存放物体移动变量
		private static var ballX:uint;
		private static var ballY:uint;
		//物体
		public var balls:Sprite;
		//跳跃控制
		public var jumpS:Boolean = true;
		
		
		public function Phypar () {//传入坐标点
			var mc:MovieClip=new MovieClip();
			mc.graphics.beginFill(0x0000ff);
			mc.graphics.drawCircle(120, 120, 20);
			mc.graphics.endFill();
			this.addChild(mc);
			mc.x=100;
			mc.y=300;
			getJump (mc,100,100);
			
			mc.addEventListener(MouseEvent.CLICK, __click);
		}
		
		private function __click(event:MouseEvent):void
		{
			var mc:MovieClip = event.target as MovieClip;
			mc.x=100;
			mc.y=300;
			getJump (mc,200, 100);
		}
		
		public function getJump (ball:MovieClip,xa:int,ya:int) {
			balls = ball;
			ballX = balls.x
			x1 = xa;
			y1 = ya;
			x3=x1+400;
			y3=y1;
			
			x2=x1+(x3-x1)/2;
			y2=y1-100;
			
			b=((y1-y3)*(x1*x1-x2*x2)-(y1-y2)*(x1*x1-x3*x3))/((x1-x3)*(x1*x1-x2*x2)-(x1-x2)*(x1*x1-x3*x3));
			a=((y1-y2)-b*(x1-x2))/(x1*x1-x2*x2);
			c=y1-a*x1*x1-b*x1;
			trace(jumpS)
			//if(jumpS){
			balls.addEventListener(Event.ENTER_FRAME,ballMoveEvent);
			//}
		}
		private function ballMoveEvent (e:Event) {
			jumpS=false;
			//trace(Math.abs(balls.y-y1));
			ballX = ballX +10;
			ballY = a*(ballX*ballX)+b*ballX+c;
			balls.x = ballX;
			balls.y = ballY;
			if (Math.abs(balls.y-y1)<15) {
				balls.y = y1;
				balls.removeEventListener (Event.ENTER_FRAME,ballMoveEvent);
				jumpS=true;
			}
		}
	}
}